package com.zrrd.map;

import java.util.Objects;

/**
 * @author WanJl
 * @version 1.0
 * @title Student
 * @description 学生类---模型类 实体类
 * @create 2024/4/21
 */
//实现Comparable接口，泛型传入要比较的对象的类型
public class Student implements Comparable<Student>{
    private String name;
    private Integer age;

    public Student() {
    }

    public Student(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
    /*
    HashMap集合中的Key如果存储我们自定义类型的元素，要想实现元素的唯一，
    就必须要重写hashCode和equals方法
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(name, student.name) && Objects.equals(age, student.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }

    @Override
    public int compareTo(Student o) {
        int r=this.getAge()-o.getAge();
        if(r==0){
            return this.getName().compareTo(o.getName());
        }
        return r;
    }
}
